package com.nstar.base.container
{
	import flash.external.ExternalInterface;
    import flash.geom.Point;
    import flash.net.navigateToURL;
    import mx.containers.Canvas;
    import mx.events.MoveEvent;
    import mx.events.ResizeEvent;
    import flash.events.Event;

	public class IFrame extends Canvas
	{
		public function IFrame() {
			super();
			
			this.addEventListener(MoveEvent.MOVE, moveIFrame);
			this.addEventListener(ResizeEvent.RESIZE, moveIFrame);
		}
		private var __source: String;

        /**
         * Move iframe through ExternalInterface.  The location is determined using localToGlobal()
         * on a Point in the Canvas.
         **/
        private function moveIFrame(event:Event): void
        {

            var localPt:Point = new Point(0, 0);
            var globalPt:Point = this.localToGlobal(localPt);

            ExternalInterface.call("moveIFrame", globalPt.x, globalPt.y, this.width, this.height);
        }

        /**
         * The source URL for the IFrame.  When set, the URL is loaded through ExternalInterface.
         **/
        public function set source(source: String): void
        {
            if (source)
            {

                if (! ExternalInterface.available)
                {
                    throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");
                }
                __source = source;
                ExternalInterface.call("loadIFrame", source);
                moveIFrame(null);
            }
        }

        public function get source(): String
        {
            return __source;
        }

        /**
         * Whether the IFrame is visible.  
         **/
        override public function set visible(visible: Boolean): void
        {
            super.visible=visible;

            if (visible)
            {
                ExternalInterface.call("showIFrame");
            }
            else 
            {
                ExternalInterface.call("hideIFrame");
            }
        }
	}
}